package net.homeip.mmhg.game;

import net.homeip.mmhg.game.GameContext.GameState;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Starts new game at set time.
 * 
 * @author pilot
 */
@Component
public class GameJob implements StatefulJob {

	private GameContext gameContext;
	
	@Autowired
	public void setGameContext(GameContext context) {
		gameContext = context;
	}
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		try {
			if (gameContext.getState() == GameState.NEW) {
				gameContext.start();
			} else {
				gameContext.stop();
			}
		} catch (Throwable throwable) {
			throw new JobExecutionException(throwable);
		}
	}
}
